﻿using UnityEngine;
using System.Collections.Generic;
using CinemaDirector.Helpers;

namespace CinemaDirector
{
    /// <summary>
    /// A custom event that will set a GameObject's specific component enable state at a given time. 
    /// </summary>
    [CutsceneItemAttribute("Cool", "ShakeCamera", CutsceneItemGenre.ActorItem)]
    public class CGShakeCamera : CinemaActorAction
    {
        // Options for reverting in editor.
        [SerializeField]
        private RevertMode editorRevertMode = RevertMode.Revert;

        // Options for reverting during runtime.
        [SerializeField]
        private RevertMode runtimeRevertMode = RevertMode.Revert;

        public Vector3 ShakeDistance;

        ShakeCamera shake;

        /// <summary>
        /// Cache the state of all actors related to this event.
        /// </summary>
        /// <returns>Info to revert rotation</returns>
        public RevertInfo[] CacheState()
        {
            List<Transform> actors = new List<Transform>(GetActors());
            List<RevertInfo> reverts = new List<RevertInfo>();
            foreach (Transform go in actors)
            {
                if (go != null)
                {
                    Transform t = go.GetComponent<Transform>();
                    if (t != null)
                    {
                        reverts.Add(new RevertInfo(this, t, "localPosition", t.localPosition));
                    }
                }
            }

            return reverts.ToArray();
        }

        public override void Trigger(GameObject actor)
        {
            shake = actor.GetComponent<ShakeCamera>();
            if (shake == null)
            {
                shake = actor.AddComponent<ShakeCamera>();
                shake.camTransform = actor.transform;
            }

            shake.Reset();
            shake.duration = Duration;
            shake.shakeDistance = ShakeDistance;
        }

        /// <summary>
        /// Continue to update the transform to look at the target.
        /// </summary>
        /// <param name="actor">The actor being updated.</param>
        /// <param name="runningTime">The running time of the cutscene.</param>
        /// <param name="deltaTime">The deltaTime since last call.</param>
        public override void UpdateTime(GameObject actor, float runningTime, float deltaTime)
        {
            if (actor == null || shake == null) return;
#if UNITY_EDITOR
            shake.Update(deltaTime);
#else
            //shake.Update();
#endif
        }

        public override void End(GameObject Actor)
        {
            if (shake != null)
            {
                shake.duration = 0;
            }
        }

        public override void Stop(GameObject Actor)
        {
            End(Actor);
        }

        /// <summary>
        /// Option for choosing when this Event will Revert to initial state in Editor.
        /// </summary>
        public RevertMode EditorRevertMode
        {
            get { return editorRevertMode; }
            set { editorRevertMode = value; }
        }

        /// <summary>
        /// Option for choosing when this Event will Revert to initial state in Runtime.
        /// </summary>
        public RevertMode RuntimeRevertMode
        {
            get { return runtimeRevertMode; }
            set { runtimeRevertMode = value; }
        }
    }
}